package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.AdminMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Admin;
import com.lemeng.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = AdminService.class)
public class AdminServiceImpl implements AdminService {
    
    @Autowired
    AdminMapper adminMapper;

    @Override
    public List<Admin> findAll() {
        return adminMapper.selectAll();
    }

    @Override
    public PageResult<Admin> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Admin> adminPageInfo = new PageInfo<>(adminMapper.selectAll());
//        Page<Admin> admins = (Page<Admin>)adminMapper.selectAll();
        return new PageResult(adminPageInfo.getTotal(),adminPageInfo.getList());
    }

    @Override
    public List<Admin> findList(Map<String, Object> serchMap) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("loginName")!=null && StringUtil.isNotEmpty(serchMap.get("loginName").toString())){
                criteria.andEqualTo("loginName",serchMap.get("loginName").toString());
            }
            if (serchMap.get("status")!=null && StringUtil.isNotEmpty(serchMap.get("status").toString())){
                criteria.andEqualTo("status",serchMap.get("status").toString());
            }
        }
        return adminMapper.selectByExample(example);
    }

    @Override
    public PageResult<Admin> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<Admin> adminPageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(adminPageInfo.getTotal(),adminPageInfo.getList());
    }

    @Override
    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Admin admin) {
        adminMapper.insert(admin);
    }

    @Override
    public void update(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }


    @Override
    @Transactional
    public void delete(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addImgItem(Admin admin) {
    }

    @Override
    public void removeImageItems(Long id,Long uid) {
    }

    @Override
    @Transactional
    public void moveAdmin(Long newId, Long oldId, Long uid) {
    }
}
